package com.demo.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Objects;

public class MultipartFileToFileByStream {
  public static File multipartFileToFileByStream(MultipartFile multipartFile) throws IOException {
    File file = new File(Objects.requireNonNull(multipartFile.getOriginalFilename()));
    try (InputStream inputStream = multipartFile.getInputStream();
         OutputStream outputStream = new FileOutputStream(file)) {
      byte[] buffer = new byte[1024];
      int length;
      while ((length = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, length);
      }
    }
    return file;
  }
}